/**
 * 
 */
package ch.baeni.jee.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import ch.baeni.jee.util.SLL_Globals;
import ch.baeni.jee.util.SLL_Globals.UserRole;

/**
 * User-Entity
 * 
 * @author Benjamin
 * 
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "Baeni_Usertable")
public class User implements Serializable {
	/**
	 * User-ID
	 */
	@Id
	@GeneratedValue
	private Long id;
	/**
	 * Benutzername
	 */
	@Column(name = "username", unique = true)
	private String username;
	/**
	 * Passwort
	 */
	@Column(name = "password")
	private String password;
	/**
	 * Aktive ja oder nein
	 */
	@Column(name = "active")
	private int active;
	/**
	 * Benutzerrolle
	 */
	@Column(name = "role")
	private String role;
	/**
	 * Vorname
	 */
	@Column(name = "firstname")
	private String firstname;
	/**
	 * Nachname
	 */
	@Column(name = "surname")
	private String surname;
	/**
	 * Adresse
	 */
	@Column(name = "address")
	private String address;
	/**
	 * Postleitzahl
	 */
	@Column(name = "zipcode")
	private String zipCode;
	/**
	 * Ort
	 */
	@Column(name = "location")
	private String location;
	/**
	 * E-Mail Adresse
	 */
	@Column(name = "email")
	private String email;
	/**
	 * Telefonnummer
	 */
	@Column(name = "phone")
	private String phone;
	/**
	 * Geburtsdatum
	 */
	@Temporal(TemporalType.DATE)
	@Column(name = "dateofbirth")
	private Date dateOfBirth;
	/**
	 * Geschlecht
	 */
	@Column(name = "gender")
	private String gender;

	/**
	 * Default Konstruktor.
	 */
	public User() {

	}

	public String getName() {
		return this.firstname + " " + this.surname;
	}

	public Long getId() {
		return id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		if (this.password == null || this.password.compareTo(password) != 0) {
			this.password = SLL_Globals.getHashCode(password);
		} else {
			this.password = password;
		}
	}

	public boolean isActive() {
		return (1 == this.active);
	}

	public void setActive(int active) {
		this.active = active;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public boolean isAdmin() {
		return (UserRole.ADMIN.name().equals(this.role));
	}

	@Override
	public String toString() {
		StringBuffer strBuff = new StringBuffer();
		strBuff.append(this.surname);
		strBuff.append(" " + this.firstname);
		strBuff.append(" (").append(this.id).append(")");
		return strBuff.toString();
	}
}